package com.bss.controller;

import com.bss.pojo.Category;
import com.bss.pojo.Company;
import com.bss.pojo.Order;
import com.bss.pojo.Product;
import com.bss.service.CategoryService;
import com.bss.service.CompanyService;
import com.bss.service.OrderService;
import com.bss.service.ProductService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sm.entity.Financial;
import com.sm.entity.UserInfo;
import com.sm.entity.UserLogin;
import com.sm.service.FinancialService;
import com.sm.service.Impl.FinancialServiceImpl;
import com.sm.utils.AttendanceUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * @Author 袁飞
 * @company 中原工学院
 * @create 2020-03-14-19:49
 */
@RestController
@RequestMapping("/bss/order")
public class OrderController {
    @Autowired
    OrderService OrderService;
    @Autowired
    CategoryService categoryService;
    @Autowired
    ProductService productService;
    @Autowired
    CompanyService companyService;
    @RequestMapping("find")
    public Order getOrder(@RequestParam("id") Integer id){
        return OrderService.findOrderById(id);
    }
    @RequestMapping("addOrUpd")
    public void addOrUpd(Order Order){
        System.out.println(Order);
        if ((Order.getOrderId()!= null)){
            OrderService.updateOrder(Order);
        }else {
            OrderService.addOrder(Order);
        }
    }
    @RequestMapping("del")
    public void delOrder(@RequestParam("id") Integer id){
        OrderService.deleteOrderById(id);
    }

    /**
     * 根据flag判断，跳转到哪里
     * @param pageNum
     * @param flag
     * @return
     */
    @RequestMapping("getAll")
    public ModelAndView getAll(@RequestParam("pageNum") Integer pageNum, @RequestParam(value = "flag",required = false) Integer flag){
        ModelAndView modelAndView = new ModelAndView();
        if(pageNum==null){
            pageNum=1;
        }
        PageHelper.startPage(pageNum,5);
        List<Order> OrderList = OrderService.findAll();
        PageInfo<Order> OrderPageInfo = new PageInfo<Order>(OrderList);
        List<Category> categoryList = categoryService.findAll();
        modelAndView.addObject("pageInfo",OrderPageInfo);
        modelAndView.addObject("categoryList",categoryList);
        if(flag!=null){
            modelAndView.setViewName("/bssmanager/order/buyAndCancel");
        }else {
            modelAndView.setViewName("/bssmanager/order/orderList");
        }
        return modelAndView;
    }
    @RequestMapping("cancel")
    public void cancelOrderById(@RequestParam("id") Integer id){
        OrderService.cancelOrder(id);
    }

    @RequestMapping("addOrder")
    public void addOrder(String productId,String companyId,String num,String totalPrice) throws Exception {
        Product product = productService.findProductById(Integer.valueOf(productId));
        Company company = companyService.findCompanyById(Integer.valueOf(companyId));
        //TODO 进货
        FinancialService financialService = new FinancialServiceImpl();
        Financial financial = new Financial();
        financial.setUpdatetime(new Date());
        financial.setIe(3);//未收
        financial.setMonth(AttendanceUtil.getMonth());
        financial.setKind(1);
        financial.setFname(company.getCompanyName()+"的订单");
        financial.setComment("无");
        financial.setAmount(new BigDecimal(totalPrice).doubleValue());
        UserLogin userLogin = (UserLogin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        UserInfo userInfo = new UserInfo();
        userInfo.setUid(userLogin.getUid());
        financial.setUserInfo(userInfo);
        Integer fid = financialService.addFinancial(financial);

        Order order = new Order();
        order.setOrderStatus(0);
        order.setOrderTag(1); //进货
        order.setOrderId(System.currentTimeMillis()/1000+"");
        order.setBusinessName(company.getCompanyName());
        order.setBusinessPhone(company.getCompanyPhone());
        order.setBusinessAddr(company.getCompanyAddr());
        order.setTotalPrice(new BigDecimal(totalPrice));
        order.setOrderType(product.getCategoryType().toString());
        order.setFinanceId(fid);
        int i = OrderService.addOrder(order);
        int productStock = product.getProductStock() + Integer.valueOf(num);
        product.setProductStock(productStock);
        int i1 = productService.updateProduct(product);  //更新库存
    }
    @RequestMapping("query")
    public ModelAndView query(String orderId,String businessName,String MaxtotalPrice,String MintotalPrice){
        ModelAndView modelAndView = new ModelAndView();
        PageHelper.startPage(1,5);
        BigDecimal max;
        BigDecimal min;
        if (MaxtotalPrice!=null && MaxtotalPrice != ""){
            max = new BigDecimal(MaxtotalPrice);
        }else {
            max = new BigDecimal("99999999");
        }
        if(MintotalPrice!=null&&MintotalPrice != ""){
            min = new BigDecimal(MintotalPrice);
        }else{
            min = new BigDecimal("0");
        }
        List<Order> orderList = OrderService.findOrder(orderId, businessName, max, min);
        PageInfo<Order> OrderPageInfo = new PageInfo<Order>(orderList);
        modelAndView.addObject("pageInfo",OrderPageInfo);
        modelAndView.setViewName("/bssmanager/order/orderList");
        return modelAndView;
    }
}
